package class29;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class MaxSlary {

    public static class Job{
        public int money;
        public int hard;

        public Job(int m,int h){
            money = m;
            hard = h;
        }
    }

    public static class MyHardC implements Comparator<Job>{

        @Override
        public int compare(Job o1, Job o2) {
            return o1.hard!=o2.hard?o1.hard-o2.hard:o2.money-o1.money;
        }
    }

    public static int[] maxSlary(Job[] job,int[] ables){
        if (job ==null|| job.length==0||ables==null||ables.length==0){
            return null;
        }
        Arrays.sort(job,new MyHardC());
        TreeMap<Integer,Integer> map = new TreeMap<>();
        Job preHard = job[0];
        map.put(preHard.hard, preHard.money);
        for (Job item:job){
            if (preHard.hard != item.hard && item.money > preHard.money){
                map.put(item.hard, item.money);
                preHard = item;
            }
        }

        int[] res = new int[ables.length];
        for (int i =0;i<ables.length;i++){
            if (map.floorKey(ables[i]) != null){
                res[i] = map.get(map.floorKey(ables[i]));
            }else {
                res[i] = 0;
            }
        }
        return res;
    }

    public static void main(String[] args) {

        Job[] jobs = new Job[]{new Job(1,2),new Job(1,3),new Job(2,2),new Job(2,4),new Job(3,5),new Job(5,3)};
        int[] ables = new int[]{1,1,2,3,4,5,6,7,8};
        int[] res = maxSlary(jobs, ables);

        for (int i : res){
            System.out.print(i+ " ");
        }
    }
}
